﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;

namespace LabMS.Web.Equipment.LowValue
{
    public partial class lowreturn : Base.BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            strPagePrivilege.Add("DZPLY");//必须写在ispostback外
            strPageUserType.Add("mana");

            tbInstrumentName.Attributes.Add("readOnly", "-1");
            tbInstrumentNum.Attributes.Add("readOnly", "-1");
            tbBorrowTime.Attributes.Add("readOnly", "-1");
            tbReturnTime.Attributes.Add("readOnly", "-1");
            tbOperator.Attributes.Add("readOnly", "-1");
            tbBorrowedState.Attributes.Add("readOnly", "-1");
            tbReturnTime.Text = DateTime.Now.ToShortDateString();

            if (Request.QueryString["ID"].ToString() == "")
            {
                Common.JShelper.JSAlert(Page, "err", "参数错误!");
                return;
            }
            string strWhere = " ID=" + Request.QueryString["ID"].ToString();
            if (!Page.IsPostBack)
            {
                LabMS.Model.Recipients mrecipients = checkQueryString(Request.QueryString["ID"].ToString());
                if (mrecipients == null)
                {
                    Common.JShelper.JSAlert(Page, "err2", "数据不存在!");
                    return;
                }
                lbPageName.Text = mrecipients.Name + "--归还";
                lbCID.Text = mrecipients.ID.ToString();
                bind(mrecipients);
            }

        }

        LabMS.Model.Recipients checkQueryString(string strID)
        {
            LabMS.BLL.Recipients brecipients = new LabMS.BLL.Recipients();
            string strWhere = " Type = 'lowvalue' and ID =" + strID;
            List<LabMS.Model.Recipients> listrecipients = brecipients.GetModelList(strWhere);
            if (listrecipients.Count <= 0)
            {
                return null;
            }
            return listrecipients[0];
        }


        public void bind(Model.Recipients aa)
        {
            tbInstrumentNum.Text = aa.Numbers;
            tbInstrumentName.Text = aa.Name;
            tbBorrowedState.Text = aa.BorrowedState;
            tbComment.Text = aa.Comment;
            tbOperator.Text = aa.Operator;
            tbRecipients.Text = aa.MRecipients;
            tbBorrowTime.Text = aa.EncodedTime.ToString();
        }

        protected void lbReturn_Click(object sender, EventArgs e)
        {
            LabMS.BLL.Recipients brecipients = new LabMS.BLL.Recipients();
            LabMS.Model.Recipients mrecipients = new LabMS.Model.Recipients();
            ExtendBLL.Recipients ebrecipients = new ExtendBLL.Recipients();
            try
            {
                mrecipients = brecipients.GetModel(long.Parse(lbCID.Text));
            }
            catch (Exception ex)
            {
                Common.JShelper.JSAlert(Page, "err2", ex.Message.ToString());
                return;
            }

            mrecipients.ReturnState = tbReturnState.Text;
            mrecipients.ReturnTime = DateTime.Parse(tbReturnTime.Text.ToString());

            try
            {
                ebrecipients.ExtendReturn(mrecipients);
                LabMS.Common.JShelper.JSAlertAndRedirect(Page, "AddSuccess", "归还成功!", "lowrecipientslist.aspx");
            }
            catch (Exception ex)
            {
                lbErr.Text = ex.Message.ToString();
            }
        }
    }
}
